package com.person.jc.web.buy;

import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.buy.BsPartsConfirmService;
import com.person.jc.service.service.BsOrderService;
import com.person.jc.service.test.inspect.BsRepairRegLogService;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.SessionKit;

/**
 * fcl业务配件确认
 */
//TODO 配件确认
public class BsPartsConfirmController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BsPartsConfirmService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> details = BsPartsConfirmService.me.getViewList(map);
		Record obj = BsOrderService.me.getOrderInfo(String.valueOf(map.get("orderId")));
		setAttr("obj", obj);
		setAttr("details", details);
		setAttr("view", getPara("view"));
		render("edit.jsp");
	}
	
	public void save(){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		String[] logId = getParaValues("logId");
		String[] confirmStatus = getParaValues("confirmStatus");
		String orderId = getPara("orderId");
		BsPartsConfirmService.me.delete(logId);
		String logIds = "";
		if(logId.length != 0){
			Object[][] params = new Object[logId.length][3];
			for (int i = 0; i < logId.length; i++) {
				params[i][0] = (logId != null && logId.length >= i ) ? logId[i] : "0";
				params[i][1] = ( confirmStatus != null && confirmStatus.length >= i )  ? confirmStatus[i] : "-1";
				params[i][2] = tsUser.getUserId();
				logIds += (logId != null && logId.length >= i ) ? logId[i]+"," : "";
			}
			BsPartsConfirmService.me.save(logIds, params, logId.length);
		}
		
		/*
		 * 判断订单是否已确认完，检查完的更新订单FLOWID;
		 */
		BsPartsConfirmService.me.changeOrderFlow(orderId);
		renderText("success");
	}
	
	public void changeParts(){
		String logId = getPara("logId");
		List<Record> list = BsRepairRegLogService.me.getFaultPartsList(logId);
		setAttr("logId", logId);
		setAttr("list", list);
		render("changeParts.jsp");
	}
	
}
